package com.jiaai.web.xiaoke;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.exception.ServerException;
import com.jiaai.family.api.TFamilyApi;
import com.jiaai.family.model.TFamilyVO;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FamilyController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/admin/family")
public class FamilyController {
    @Reference
    private TFamilyApi tFamilyApi;
    /**
     * 获取所有家族列表
     * @param curr
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAll")
    @ResponseBody
    public Object findByAll(@RequestParam(defaultValue = "0", required = false) Integer curr, TFamilyVO tFamilyVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyApi.findByAll(curr, tFamilyVO));
    }
    /**
     * 获取单个家族
     * @param fyId
     * @return
     * @throws Exception
     */
    @PostMapping("/findById")
    public Object findById(Integer fyId ) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tFamilyApi.findById(fyId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 统计单个家族所有成员
     * @param fyId
     * @return
     * @throws Exception
     */
    @GetMapping("/count")
    public Object count(Integer fyId ) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tFamilyApi.count(fyId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 删除单个家族
     * @param fyId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public Object delete(Integer fyId ) throws ServerException {
        try {
            tFamilyApi.delete(fyId);
            return RestResultGenerator.genSuccessResult("删除成功");
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加编辑家族
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @PostMapping("/edit")
    public Object edit(@RequestBody TFamilyVO tFamilyVO ) throws ServerException {
        try {
            tFamilyApi.update(tFamilyVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }
    /**
     * 获取单个家族下面的所有成员
     * @param curr
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAllRole")
    @ResponseBody
    public Object findByAllRole(@RequestParam(defaultValue = "0", required = false) Integer curr, TFamilyVO tFamilyVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyApi.findByAllRole(curr, tFamilyVO));
    }
    /**
     * 修改申请家族状态
     * @param fyId
     * @param fyState
     * @return
     * @throws Exception
     */
    @PostMapping("/changeState")
    @ResponseBody
    public Object changeState(Integer fyId,Integer fyState) throws ServerException {
        try {
            tFamilyApi.changeState(fyId,fyState);
            return RestResultGenerator.genSuccessResult("修改成功");
        } catch (Exception e) {
            throw new ServerException("修改失敗");
        }
    }
}
